package com.icas.service;

import com.icas.entity.databaseentity.AllCourseEntity;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

import javax.annotation.PostConstruct;

import java.util.List;

import static com.icas.util.Constant.COURSE_TABLE_NAME;

/**
 * @Author: tocchen
 * @Date: 2022/4/18 23:17
 * @Description
 **/
@Component
public class CourseService {
    @Autowired
    private MongoTemplate mongoTemplate;

    public static CourseService db;

    public static final CourseService defaultUserServer = new CourseService();

    @PostConstruct
    public void init() {
        db = this;
        db.mongoTemplate = this.mongoTemplate;
    }

    /**
     * 通过课程id查询课程信息
     * @param id 课程id
     * @return 查询所得的结果级
     */
    public AllCourseEntity byIdGetInfo(String id){
        Query query = new Query();
        query.addCriteria(Criteria.where("_id").is(new ObjectId(id)));
        AllCourseEntity allCourseEntities = db.mongoTemplate.findOne(query, AllCourseEntity.class, COURSE_TABLE_NAME);
        return ObjectUtils.isEmpty(allCourseEntities)?null:allCourseEntities;
    }
}
